package common.store.admin.dao;


import common.store.common.util.C3p0Pool;
import common.store.web.bean.Supplier;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

public class SupplierDao {

    static QueryRunner qr;

    static {
        qr = new QueryRunner(C3p0Pool.getDataSource());

    }

    public List<Supplier> getSupplierList() {
        try {
            return qr.query("select s.s_id , s.sname from supplier s ", new BeanListHandler<>(Supplier.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return Collections.EMPTY_LIST;
    }


    public Integer delBySid(String sid) {
        try {
            return qr.update("delete from supplier  where s_id = ?", sid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public Integer addSupplier(Supplier supplier) {
        try {
            return qr.update("insert into supplier values(? , ?)", supplier.getSid(), supplier.getsName());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


    public Integer updateSupplier(String sid, String sName) {
        try {
            return qr.update("update supplier set sname = ? where s_id = ?", sName, sid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public Supplier getSupplierBySid(String sid) {

        try {
            //返回一条分类信息记录
            return qr.query("select s.s_id , s.sname from supplier s where s.s_id = ?" , new BeanHandler<>(Supplier.class) , sid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
